﻿window.onload = function () {
  //获取课程信息列表
  getCourseList()
}

//获取课程信息列表
function getCourseList() {
  $.get("cotro/admin/getCourse.ashx", function (res, status) {
    res = JSON.parse(res);
    //console.log(res);
    //判断是否有数据,没有就返回
    if (res.code == -1 || res.courses == null) {
      return;
    }
    //获取table对象
    let courseTable = document.getElementsByClassName("course-warp");
    courseTable[0].innerHTML = '' +
      '<thead><tr>' +
      '<th scope="col">课程号</th>' +
      '<th scope="col">课程名</th>' +
      '<th scope="col">课程学分</th>' +
      '<th scope="col">课程性质</th>' +
      '<th scope="col">所属学院</th>' +
      '<th scope="col">详细信息</th>' +
      '<th scope="col">修改课程描述</th>' +
      '<th scope="col">删除课程</th>' +
      '</tr></thead>';
    //存放至全局
    window.courses = res.courses;
    courseTable[0].innerHTML += addCourseListToHTML(res.courses);
  })
}

//将Course数据转换为HTML
function addCourseListToHTML(courses) {
  let s = '<tbody>'
  for (let i = 0; i < courses.length; ++i) {
    if (courses.is_deleted == 1) continue;
    s += '' +
      '<tr>' +
      '<td>' + courses[i].cour_id + '</td>' +
      '<td>' + courses[i].title + '</td>' +
      '<td>' + courses[i].credit + "学分" + '</td>' +
      '<td>' + (courses[i].attribute == 1 ? "专选课" : "公选课") + '</td>' +
      '<td>' + courses[i].faculty + '</td>' +
      '<td>' + '<a id="detail-course-info"  href="#"  onclick="addCourseModal(' + courses[i].cour_id + ')" tabindex="-1" aria-disabled="true" data-toggle="modal" data-target="#detailCourseModal">详细信息</a>' + '</td>' +
      '<td>' + '<a id="detail-course-info"  href="#" onclick="addChangeDetailModal(' + courses[i].cour_id + ')" tabindex="-1" aria-disabled="true" data-toggle="modal" data-target="#changeCourseModal">修改课程描述</a>' + '</td>' +
      '<td>' + '<a id="delete-course"  href="" onclick="deleteCourse(' + courses[i].cour_id + ')" tabindex="-1" aria-disabled="true" style="color: red;">删除课程</a>' + '</td>' +
      '</tr>';
  }
  s += '</tbody>';
  return s;
}

//删除课程函数
function deleteCourse(cour_id) {
  if (confirm("确定要删除吗？")) {
    $.post("cotro/admin/deleteCourse.ashx", {
      id: cour_id
    }, function (res, status) {
      if (status == "success") {
        alert("删除成功!");
      }
    });
    //TODO 删除列(不用刷新页面)

  }
}

//提交课程信息 
function commitCourseInfo() {
  //TODO 增加输入框的可是限制
  title = $("#course_title").val();
  credit = $("#course_credit").val();
  hour = $("#course_hour").val();
  attribute = ($("#course_attribute").val() == "公选课" ? 0 : 1);
  proportion = $("#course_proportion").val();
  faculty = $("#course_faculty").val();
  comment = $("#course_comment").val();

  $.post("cotro/admin/addCourse.ashx", {
    title: title,
    credit: credit,
    hour: hour,
    attribute: attribute,
    proportion: proportion,
    faculty: faculty,
    comment: comment,
    cover: null,
    status: "1"
  }, function (res, status) {
    if (status == "success") {
      alert("添加成功!");
      window.location = window.location;
      return;
    }
  });
}

//修改课程信息
function changeInfoOfCourse(cour_id) {
  if (confirm("确定要修改吗？")) {
    title = $("#course_title-cd").val();
    credit = $("#course_credit-cd").val();
    hour = $("#course_hour-cd").val();
    attribute = ($("#course_attribute-cd").val() == "公选课" ? 0 : 1);
    proportion = $("#course_proportion-cd").val();
    faculty = $("#course_faculty-cd").val();
    comment = $("#course_comment-cd").val();
    $.post("cotro/admin/changeCourseInfo.ashx", {
      cour_id:cour_id,
      title: title,
      credit: credit,
      hour: hour,
      attribute: attribute,
      proportion: proportion,
      faculty: faculty,
      comment: comment,
      cover: null,
      status: "1"
    }, function (res, status) {
      location.reload();
    })
  }
}

//添加可修改详细信息模态框
function addChangeDetailModal(cour_id) {
  for (i = 0, len = window.courses.length; i < len; i++) {
    cour = window.courses[i];
    if (cour_id == cour.cour_id) {
      //生成内部内容
      s = '<form>' +
        '                        <div class="form-group">' +
        '                            <label for="course_title">课程名称</label>' +
        '                            <input type="text" class="form-control" id="course_title-cd" aria-describedby="none"  value="' + cour.title + '"/>' +
        '                        </div>' +
        '                        <div class="form-group">' +
        '                            <label for="course_title">课程学分</label>' +
        '                            <input type="text" class="form-control" id="course_credit-cd" aria-describedby="none"  value="' + cour.credit + '"/>' +
        '                        </div>' +
        '                        <div class="form-group">' +
        '                            <label for="course_hour">学时</label>' +
        '                            <input type="text" class="form-control" id="course_hour-cd" aria-describedby="none"   value="' + cour.hour + '"/>' +
        '                        </div>' +
        '                        <div class="form-group">' +
        '                            <label for="course_attribute">选择课程类型</label>' +
        '                            <select class="form-control" id="course_attribute-cd" >' +
        '                                <option value="' + cour.attribute + '" selected="selected">' + (cour.attribute == 0 ? "公选课" : "专选课") + '</option>                       ' +
        '                                <option>公选课</option>' +
        '                                <option>专选课</option>' +
        '                            </select>' +
        '                        </div>' +
        '                        <div class="form-group">' +
        '                            <label for="course_proportion">课程成绩占比</label>' +
        '                            <input type="text" class="form-control" id="course_proportion-cd" aria-describedby="none"  value="' + cour.proportion + '"/>' +
        '                        </div>' +
        '                        <div class="form-group">' +
        '                            <label for="course_faculty">课程所属院系</label>' +
        '                            <input type="text" class="form-control" id="course_faculty-cd" aria-describedby="none"  value="' + cour.faculty + '"/>' +
        '                        </div>' +
        '                        <div class="form-group">' +
        '                            <label for="course_comment">课程简介</label>' +
        '                            <textarea class="form-control" id="course_comment-cd" rows="3" >' + cour.comment + '</textarea>' +
        '                        </div>' +
        '                    </form>'
      //设置
      detail_modal = document.getElementById("changeDetail-modal");
      detail_modal.innerHTML = s;
      detail_modal = document.getElementById("changeDetail-modal-footer");
      detail_modal.innerHTML = '<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>' +
                               '<button type="button" class="btn btn-primary" onclick="changeInfoOfCourse('+cour.cour_id+')">提交</button>';
      return;
    }
  }
}

//添加指定课程详细信息模态框
function addCourseModal(cour_id) {
  for (i = 0, len = window.courses.length; i < len; i++) {
    cour = window.courses[i];
    if (cour_id == cour.cour_id) {
      //生成内部内容
      s = '<form>' +
        '                        <div class="form-group">' +
        '                            <label for="course_title">课程名称</label>' +
        '                            <input type="text" class="form-control" id="course_title-d" aria-describedby="none" disabled="true" value="' + cour.title + '"/>' +
        '                        </div>' +
        '                        <div class="form-group">' +
        '                            <label for="course_title">课程学分</label>' +
        '                            <input type="text" class="form-control" id="course_credit-d" aria-describedby="none" disabled="true" value="' + cour.credit + '"/>' +
        '                        </div>' +
        '                        <div class="form-group">' +
        '                            <label for="course_hour">学时</label>' +
        '                            <input type="text" class="form-control" id="course_hour-d" aria-describedby="none"  disabled="true" value="' + cour.hour + '"/>' +
        '                        </div>' +
        '                        <div class="form-group">' +
        '                            <label for="course_attribute">选择课程类型</label>' +
        '                            <select class="form-control" id="course_attribute-d" disabled="true">' +
        '                                <option value="' + cour.attribute + '" selected="selected">' + (cour.attribute == 0 ? "公选课" : "专选课") + '</option>                       ' +
        '                                <option>公选课</option>' +
        '                                <option>专选课</option>' +
        '                            </select>' +
        '                        </div>' +
        '                        <div class="form-group">' +
        '                            <label for="course_proportion">课程成绩占比</label>' +
        '                            <input type="text" class="form-control" id="course_proportion-d" aria-describedby="none" disabled="true" value="' + cour.proportion + '"/>' +
        '                        </div>' +
        '                        <div class="form-group">' +
        '                            <label for="course_faculty">课程所属院系</label>' +
        '                            <input type="text" class="form-control" id="course_faculty-d" aria-describedby="none" disabled="true" value="' + cour.faculty + '"/>' +
        '                        </div>' +
        '                        <div class="form-group">' +
        '                            <label for="course_comment">课程简介</label>' +
        '                            <textarea class="form-control" id="course_comment-d" rows="3" disabled="true">' + cour.comment + '</textarea>' +
        '                        </div>' +
        '                    </form>'
      //设置
      detail_modal = document.getElementById("detail-modal");
      detail_modal.innerHTML = s;
      return;
    }
  }
}